package griffin.website;


//import griffin.website.TopHome.TouchItemClickListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import CIT.Test.GalleryFlow;
import CIT.Test.ImageAdapter;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
//import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
//import android.widget.ImageView;
import android.widget.Toast;

public class CoverFlow extends Activity {
	private static final String TAG = "[coverflow]";
	private List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
	private Integer[] images={R.drawable.bookmarksilver, R.drawable.filesystemfolderopen,
			R.drawable.miscellaneous,R.drawable.packagesettings};
	private String[] imagesTitle = {"Living","Leisure","Tools","Region"}; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		Log.d(TAG, "[onCreate...] ");	
        super.onCreate(savedInstanceState);
        Init();
	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) 
	{
		super.onConfigurationChanged(newConfig);
		// TODO Auto-generated method stub		
		Log.d(TAG,"[onConfigurationChanged...]" );
		Log.d(TAG, "orientation = " + this.getResources().getConfiguration().orientation);
		Init();
	}
	
	@Override
	public void onResume() 
	{
		super.onResume();
//		Log.d(TAG,"[onResume...]" );
//		Init();
		
	}
	
	@Override
	public void onDestroy() 
	{
		super.onDestroy();

//		userLogic.loginOutWhenExit();
		Log.d(TAG,"[onDestroy...]" );
	}
	
	public void Init()
	{
//      setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			Log.d(TAG,"[onConfigurationChanged:====LANDSCAPE");

	        setContentView(R.layout.coverflow);	        
	        prepareCategoryData();	        
			coverflowL();	
		}else{
			Log.d(TAG,"[onConfigurationChanged:====PORTRAIT");			
//	        setContentView(R.layout.tophome);
			
			//add 
			goTopHome();
		}
	}

	private void goTopHome()
	{
    	Intent intent =new Intent();
    	intent.putExtra("winname", "TopHome"); 
    	intent.setClass(CoverFlow.this, TabBarFramework.class);
    	startActivity(intent);
    	
    	this.finish();
	}
	
	private void coverflowL()
	{   
        ImageAdapter adapter = new ImageAdapter(this, images);
        adapter.createReflectedImages();

        GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);
        galleryFlow.setAdapter(adapter);
        
        galleryFlow.setCallbackDuringFling(true);
        galleryFlow.setOnItemClickListener(new GalleryFlow.OnItemClickListener(){
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
            {
	        	Log.d(TAG,"[Position :( "+ position+")");	        	
	        	gotoSubCategory(position);
            }
        });
        galleryFlow.setOnItemSelectedListener(new GalleryFlow.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
            	Toast.makeText(CoverFlow.this, imagesTitle[position],Toast.LENGTH_SHORT).show();
            }

            public void onNothingSelected(AdapterView<?> arg0) {
            	int i=0;
            	i++;
            }
        });
    }
	
	String[] imgTags={"1,Living","2,Leisure","3,Tools","4,Region"};
	
	private void gotoSubCategory(int position)
	{		
		 HashMap<String, Object> item=(HashMap<String, Object>) list.get(position);   
		 DisplayToast((String)item.get("ItemText"));
		 	 
		 String strCategorys=imgTags[position].toString();
		 String[] strCategory=strCategorys.split(",");
		 saveCurCategory(strCategory[0], strCategory[1]);
		 
		 Log.d("ME", strCategory[0]+"="+strCategory[1]);
		 
		 Intent intent = new Intent();
		 intent.putExtra("winname", "CategoryList"); 
		 intent.setClass(CoverFlow.this, TabBarFramework.class);
		 startActivity(intent);
	}
	
	private void saveCurCategory(String curCategoryID, String curCategoryName) 
	{
		SharedPreferences loginInfo=getSharedPreferences("griffin.website.curcate", 0);
    	SharedPreferences.Editor editor=loginInfo.edit();
    	editor.putString("curcategoryid", curCategoryID); 	
    	editor.putString("curcategoryname", curCategoryName); 	
    	editor.commit();
	}
	
 	public void DisplayToast(String str)
 	{
 		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
 	}
	
	private void prepareCategoryData()
	{
	    String databaseFilename = TabBarFramework.DATABASE_PATH + "/" + TabBarFramework.DATABASE_FILENAME;
	    SQLiteDatabase db;
	    
	    db = SQLiteDatabase.openOrCreateDatabase(databaseFilename, null);
	    Cursor result=db.rawQuery("SELECT VG_ID,VG_ENGLISH,VG_IMAGEURL FROM VG_TopCategories", null); 
	    if (result.moveToFirst())
	    {
	    	int index =0;
	        while (!result.isAfterLast()) { 
	        	String categoryid = result.getString(0);
	            String category =result.getString(1);
	            int imgurl=images[index++];
	        	Map<String, Object> map = new HashMap<String, Object>();
	        	map.put("ItemID", categoryid);
	        	map.put("ItemText", category);
	        	map.put("ItemImage", imgurl);
	        	list.add(map);
	            result.moveToNext(); 
	          }
	    }
	    result.close(); 
	    db.close();
	}
}